package com.csoa.community.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import com.csoa.community.singletons.LogInfo;

public class DatabaseHelper extends SQLiteOpenHelper {
	public static final String TAG = "DatabaseHelper";

	public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		create(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public static void create(SQLiteDatabase db) {
		createLogin(db);
		createUser(db);
		createArticle(db);
		createArticleImg(db);
		createMsgList(db);
		createMsgCmtList(db);
		createUserArticleCrpding(db);
		createUserArticleLike(db);
		createUUID(db);
		createLocationTable(db);
		createCaoGao(db);
	}

	private static boolean createCaoGao(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		try {
			db.execSQL("CREATE TABLE caogaoTable (" + "aaid TEXT PRIMARY KEY, " + "uid TEXT," + //
					"tag TEXT ," + //
					"atuser TEXT ," + // 对方的uuid
					"position TEXT ," + //
					"location TEXT ," + //
					"haveimg BOOLEAN ," + // 是否有图片，用来标记是发图片博文还是发@博文
					"imagepath TEXT ," + //
					"createtime TEXT ," + //
					"description TEXT ," + //
					"pid TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table caogaoTable ok");
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			LogInfo.LogOut(TAG + " Create Table caogaoTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 * 
	 */
	private static boolean createLogin(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE loginInfoTable (" + "uuID TEXT PRIMARY KEY, " + "email TEXT," + //
					"password TEXT ," + //
					"autoLogin TEXT ," + //
					"currentLogin TEXT ," + //
					"authen TEXT ," + //
					"loginTime TEXT ," + //
					"nickName TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table loginInfoTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table loginInfoTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 *
	 */
	private static boolean createUser(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE userInfoTable (" + "uuID TEXT PRIMARY KEY, " + "nickName TEXT," + //
					"domain TEXT ," + //
					"email TEXT ," + //
					"mobile TEXT ," + //
					"station TEXT ," + //
					"logo TEXT ," + //
					"gender TEXT ," + //
					"city TEXT ," + //
					"location TEXT ," + //
					"currentLogin TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table userInfoTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table userInfoTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 * 
	 */
	private static boolean createArticle(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE articleTable (" + "aaID TEXT PRIMARY KEY, " + "uuID TEXT," + //
					"title TEXT ," + //
					"summary TEXT ," + //
					"filter TEXT ," + //
					"imgType TEXT ," + //
					"ctime TEXT ," + //
					"status TEXT ," + //
					"isFW TEXT ," + //
					"tags TEXT ," + //
					"aboutUUID TEXT ," + //
					"forwarding TEXT ," + //
					"comment TEXT ," + //
					"likes TEXT ," + //
					"share TEXT ," + //
					"aboutID TEXT," + //
					"source TEXT," + //
					"location TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table articleTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table articleTable err,table exists." + e.getMessage());
		}
		return false;
	}

	private static boolean createLocationTable(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE locationTable (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "uuID TEXT," + //
					"longitude TEXT ," + //
					"latitude TEXT ," + //
					"nation TEXT ," + //
					"province TEXT ," + //
					"city TEXT ," + //
					"district TEXT ," + //
					"area TEXT ," + //
					"position TEXT ," + //
					"count TEXT " + //
					");");
			LogInfo.LogOut(TAG + " Create Table locationTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table locationTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 *
	 */
	private static boolean createArticleImg(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE articleImgTable (" + "aaID TEXT PRIMARY KEY, " + "original TEXT," + //
					"large TEXT ," + //
					"middle TEXT ," + //
					"thumbnail TEXT ," + //
					"album TEXT ," + //
					"extrasmall TEXT ," + //
					"smallsquare TEXT ," + //
					"square TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table articleImgTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table articleImgTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 * ����˽��
	 */
	private static boolean createMsgList(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE msgListTable (" + "msgid TEXT PRIMARY KEY, " + "posterid TEXT,"
					+ "receiverID TEXT," + //
					"direct TEXT ," + //
					"time TEXT ," + //
					"content TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table msgListTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table msgListTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 * ����˽�����۱�
	 */
	private static boolean createMsgCmtList(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE msgCmtListTable (" + "cmtID TEXT PRIMARY KEY, " + "cmtUUID TEXT," + //
					"cmtTime TEXT ," + //
					"cmtCotent TEXT ," + //
					"cmtArticleID TEXT ," + //
					"cmtRootContent TEXT ," + //
					"userID TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table msgCmtListTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table msgCmtListTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 * ����timeline�û������µĹ�ϵ��Ӧ��
	 */
	private static boolean createUserArticleCrpding(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE userArticleCrpdingTable (" + "ID INTEGER PRIMARY KEY, " + "uuID TEXT," + //
					"aaID TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table userArticleCrpdingTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table userArticleCrpdingTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 */
	private static boolean createUserArticleLike(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE userLikeTable (" + "aaID TEXT PRIMARY KEY, " + "uuID TEXT" + //
					");");
			LogInfo.LogOut(TAG + " Create Table userLikeTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table userLikeTable err,table exists." + e.getMessage());
		}
		return false;
	}

	/**
	 * 创建UUID表 通过用户uuid查询对应文章aaid(获取首页信息时，用于从数据库中查询相应用户的文章)
	 * 
	 * @return
	 */
	private static boolean createUUID(SQLiteDatabase db) {
		try {
			db.execSQL("CREATE TABLE UUIDTable (" + "uuID TEXT," + //
					"aaID TEXT PRIMARY KEY" + //
					");");
			LogInfo.LogOut(TAG + " Create Table UUIDTable ok");
			return true;
		} catch (Exception e) {
			LogInfo.LogOut(TAG + " Create Table UUIDTable err,table exists." + e.getMessage());
		}
		return false;
	}

}
